/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.charge.dao.ICharStoChoGeneralDao;
import com.ce.pms.charge.model.CharStaChoDomain;
import com.ce.pms.charge.vo.ChargeStaChoVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：常规费用<br>
 * @author 罗军林 创建日期：2008-8-15
 */
public class CharStoChoGeneralDaoImpl extends PmsBaseDaoImpl<CharStaChoDomain> implements
		ICharStoChoGeneralDao {

	/**
	 * 功能描述:查找指定房号的所有房间标准选用<br>
	 * @param houseId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-22<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public List<CharStaChoDomain> queryCharStaChoByHouseId(Long houseId){
		String hql="FROM CharStaChoDomain cho where cho.house.keyword=?";
		Query query=createQuery(hql, houseId);
		return query.list();
	}

	public void addCharStaCho(ChargeStaChoVO charStaChoVO,Map<Long,BigDecimal> chargeAmts) {
		String sql="INSERT INTO FEE_CHRG_STA_CHO(HOUSE_ID, CHARGE_ITEM_ID, CHARGE_STA_ID, CHARGE_AMT, CREATOR, SYSDATE)" +
		" VALUES(?,?,?,?,?,now()) ";

		List<Object[]> list=new ArrayList<Object[]>();
		int i=0;
		for(Long houseId : chargeAmts.keySet()){
			Object[] args={houseId,
					charStaChoVO.getChargeItemID(),
					charStaChoVO.getChargeStaID(),
					chargeAmts.get(houseId),
					charStaChoVO.getCreater()};
			list.add(args);

			if( ((i+1) % 50 ==0) || i==(chargeAmts.size()-1) ){
				this.executeSQLBatch(sql, list);
				list.clear();
			}
			i++;
		}
	}

	/**
	 * 功能描述:检查指定房间是否选用过指定类型的收费项目<br>
	 * @param chargeType
	 * @param houseId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public boolean checkExistsCharStaCho(String chargeType,Long houseId){
		String hql="FROM CharStaChoDomain cho WHERE cho.house.keyword=? AND cho.chargeItem.chrgType=? ";
		Query query=createQuery(hql, new Object[]{houseId,chargeType});
		List list=query.list();
		return list.size()>0;
	}

	/**
	 * 功能描述:获取指定收费标准且已经进行房间标准选用的房号ID,返回ID串。如 1,2,3<br>
	 * @param charStaId
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public String getHouseIdsByCharStaId(Long charStaId){
		String sql="SELECT GROUP_CONCAT(CAST(HOUSE_ID as char) SEPARATOR ',') FROM FEE_CHRG_STA_CHO WHERE CHARGE_STA_ID=? GROUP BY CHARGE_STA_ID";
		Query query=createSQLQuery(sql, charStaId);
		return (String)query.uniqueResult();
	}

	/**
	 * 功能描述:更新指定标准、房间的费用。<br>
	 * @param chargeStaId
	 * @param chargeAmts 房间与费用的MAP<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-10<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public void updateCharStoAmt(Long chargeStaId,Map<Long,BigDecimal> chargeAmts){
		String sql="UPDATE FEE_CHRG_STA_CHO SET CHARGE_AMT=? WHERE CHARGE_STA_ID=? AND HOUSE_ID=?";
		List<Object[]> parameter=new ArrayList<Object[]>();
		for(Long houseId : chargeAmts.keySet()){
			parameter.add(new Object[]{chargeAmts.get(houseId),chargeStaId,houseId});
		}
		executeSQLBatch(sql, parameter);
	}

	public Boolean checkExistCharStoChoGeneral(ChargeStaChoVO condition,ChargeStaChoVO oldCondition) {
		Map<String, Long> parameter = new HashMap<String, Long>();
		StringBuffer sql=new StringBuffer("SELECT COUNT(*) FROM CharStaChoDomain char ")
		.append(" WHERE 1=1 ")
		.append(builderParameter(condition,oldCondition,parameter));

		Query query=this.createNameQuery(sql.toString(), parameter);
		Long result=(Long)query.uniqueResult();

		return result>0;
	}


	public Page queryCharStoCho(ChargeStaChoVO condition) {
		Map<String, Object> paraFixed = new HashMap<String, Object>();

		StringBuffer sql = new StringBuffer("SELECT charstacho FROM CharStaChoDomain charstacho,FlatDomain flat WHERE charstacho.house.id=flat.id ");
		sql.append(buildCondition(condition,paraFixed));
		sql.append(buildOrder(condition));
		return this.pagedNameQueryByPageNo(sql.toString(), condition.getPageNo(), condition.getPageSize(),paraFixed);
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.dao.ICharStoChoGeneralDao#checkExistCharStoChoByStaId(java.lang.Long)
	 */
	public boolean checkExistCharStoChoByStaId(Long staId) {
		String sql="SELECT COUNT (*)FROM CharStaChoDomain cho where cho.chargeSta.keyword=?";
		Query query = createQuery(sql, staId);
		Long result=(Long)query.uniqueResult();
		return result>0;
	}

	/**
	 * 功能描述:构建排序条件<br>
	 * @param condition
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-19<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildOrder(ChargeStaChoVO condition){
		if(condition.getSortStr()==null || condition.getSortStr().trim().length()<1){
			return " ORDER BY charstacho.house.building.seqNo,flat.floor,charstacho.house.houseNumber ";
		}else if("SYSDATE".indexOf(condition.getSortStr().trim().split(" ")[0].toUpperCase())>=0){
			return " ORDER BY charstacho.sysInfo.sysDate DESC" ;
		}else{
			return " ORDER BY " + condition.getSortStr() ;
		}
	}

	/**
	 * 功能描述:构建查询条件<br>
	 * @param condition
	 * @param paraFixed
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-18<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String buildCondition(ChargeStaChoVO condition,Map<String,Object> paraFixed){
		StringBuffer result=new StringBuffer();
		if(condition == null){
			return "";
		}

		result.append(" AND charstacho.chargeItem.chrgType=:chrgType ");
		paraFixed.put("chrgType", SysrefcodeDO.FEE_CHAR_TYPE_CLASS_CODE_GENERAL);

		if(condition.getBuildingID() != null){
			result.append(" AND charstacho.house.building.keyword = :buildingID ");
			paraFixed.put("buildingID", condition.getBuildingID() );
		}

		if(condition.getChargeItemID() != null){
			result.append(" AND charstacho.chargeItem.keyword = :chargeItemID" );
			paraFixed.put("chargeItemID", condition.getChargeItemID() );
		}

		if(condition.getChargeStaID() != null){
			result.append(" AND charstacho.chargeSta.keyword = :chargeStaID ");
			paraFixed.put("chargeStaID", condition.getChargeStaID() );
		}

		if(condition.getHouseNumber() != null && condition.getHouseNumber().trim() != ""){
			result.append(" AND charstacho.house.houseNumber like :houseNumber ");
			paraFixed.put("houseNumber", "%"+condition.getHouseNumber()+"%");
		}

		return result.toString();
	}

	/**
	 * 功能描述:构建唯一验证的查询条件<br>
	 * @param charStaChoMeterVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-13<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String  builderParameter(ChargeStaChoVO searchCondition,ChargeStaChoVO oldCondition,Map<String, Long> parameter) {
		StringBuffer result=new StringBuffer();
		if(searchCondition!=null && searchCondition.getHouseId()!=null){
			parameter.put("hid", searchCondition.getHouseId());
			result.append(" AND char.house.keyword=:hid ");
		}
		if(searchCondition!=null && searchCondition.getChargeItemID()!=null){
			parameter.put("itemid", searchCondition.getChargeItemID());
			result.append(" AND char.chargeItem.keyword=:itemid ");
		}

		if(oldCondition!=null && oldCondition.getHouseId()!=null){
			parameter.put("oldHid", searchCondition.getHouseId());
			result.append(" AND char.house.keyword<>:oldHid ");
		}
		if(oldCondition!=null && oldCondition.getChargeItemID()!=null){
			parameter.put("oldItemid", oldCondition.getChargeItemID());
			result.append(" AND char.chargeItem.keyword<>:oldItemid ");
		}
		return result.toString();
	}


}
